package com.sykj.why.controller.front.programa;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.ProgramaTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Eleven
 * @date 2019/6/18
 * @desc 前端栏目控制访问层
 */

@RestController
@RequestMapping("/front/programa")
@Slf4j
@Api(value = "/front/programa",description = "前端栏目控制访问层")
public class FrontProgramaController {

    @Autowired
    private ProgramaTService programaTService;

    /**
     * 查询栏目列表
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "/findAll",notes = "查询栏目列表")
    public ResponseEntity findAll(){
        try {
            return Response.success("查询成功", programaTService.findAll());
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询栏目列表出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 根据站点地址查询站点Id
     * @param code
     * @return programaId
     */
    @RequestMapping(value = "/findByUrl/{code}",method = RequestMethod.GET)
    @ApiOperation(value = "/findByUrl/{code}",notes = "根据站点地址查询站点Id")
    public ResponseEntity findByUrl(@PathVariable String code){
        try {
            return Response.success("查询成功",programaTService.findByCode(code));
        } catch (WhyException e){
            return Response.serverError(e.getMessage(),e.getCode());
        } catch (Exception e){
            log.error("查询站点ID出现异常,{}",e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(),ResultEnum.UNERROR.getCode());
        }
    }

}
